package com.dreamweaver.mvvmtest.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dreamweaver.LiveDataStore;
import com.dreamweaver.mvvmtest.R;
import com.dreamweaver.mvvmtest.bean.User;
import com.dreamweaver.mvvmtest.model.UserModel;
import com.dreamweaver.mvvmtest.widget.LifecycleView;

public class UserActivity extends AppCompatActivity {
    UserModel userModel;
    @BindView(R.id.txt)
    TextView txt;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.lv)
    LifecycleView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        userModel = new ViewModelProvider(this).get(UserModel.class);
        userModel.getUserLiveData().observe(this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                showUserInfo(user);
            }
        });


    }

    private void showUserInfo(User user) {
        txt.setText("序号：" + user.getId() + "；名称：" + user.getName());
    }

    @OnClick({R.id.btn, R.id.btn_refresh_lv, R.id.btn_disable_lv})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                userModel.refreshData();
                break;
            case R.id.btn_refresh_lv:
                LiveDataStore.getIntegerMutableLiveData().postValue((int) (Math.random() * 1000));
                break;
            case R.id.btn_disable_lv:
                lv.disable();
                break;
            default:
                break;
        }
    }
}
